
<ValidationProperty("Text")> _
Partial Class WebForm_CommonControl_DropDown
    Inherits System.Web.UI.UserControl
    Implements System.Web.UI.ITextControl, ITextControlExtension
        
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
            If AutoPostBack = true Then
                'Dim script As String = String.Format("$get('{0}').change();", txt.ClientID)
            	Dim script As String = String.Format("__doPostBack('{0}','');", txt.ClientID)
            	
            	pe.CommitScript = script
            End If 

    End Sub
    
    Public Property Text() As String Implements ITextControl.Text
        Get
            '  myobj.writelog("a")
            If PopupControl IsNot Nothing Then
                Return TryCast(PopupControl, ITextControlExtension).FindValue(txt.Text)
            End If

            Return txt.Text
        End Get
        Set(ByVal value As String)
            '  myobj.writelog("b")
            If PopupControl IsNot Nothing Then
                value = TryCast(PopupControl, ITextControlExtension).FindText(value)
            End If

            txt.Text = value
        End Set
    End Property

    Public Property TargetControlID() As String
        Get
            Return pe.TargetControlID
        End Get
        Set(ByVal value As String)
            pe.TargetControlID = value
        End Set
    End Property

    Public Property PopupControlID() As String
        Get
            Return pe.PopupControlID
        End Get
        Set(ByVal value As String)
            pe.PopupControlID = value
        End Set
    End Property

    Public ReadOnly Property PopupControl() As Control
        Get
            Dim p As Control = NamingContainer

            While True
                If p Is Nothing Then
                    Return Nothing
                End If

                If p.FindControl(PopupControlID) IsNot Nothing Then
                    Dim s As New Stack

                    s.Push(p.FindControl(PopupControlID))

                    While s.Count > 0
                        Dim pp As Control = s.Pop

                        For Each c As Control In pp.Controls

                            If c IsNot Me AndAlso GetType(ITextControlExtension).IsAssignableFrom(c.GetType()) Then
                                Return c
                            End If

                            If c.Controls.Count > 0 Then
                                s.Push(c)
                            End If
                        Next
                    End While
                End If

                p = p.NamingContainer
            End While

            Return Nothing
        End Get
    End Property
    
    Public Property AutoPostback() As Boolean
        Get
            Return txt.AutoPostback
        End Get
        Set(ByVal value As Boolean)
        
            txt.AutoPostback = value            
                       
        End Set
    End Property
    
    Protected Sub txt_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt.TextChanged

            
        RaiseEvent SelectedIndexChanged(Me, EventArgs.Empty)
    End Sub

    Public Event SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

    Public Property SelectedText() As String Implements ITextControlExtension.SelectedText
        Get
            Return txt.Text
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Function FindValue(ByVal text As String) As String Implements ITextControlExtension.FindValue
        Return String.Empty
    End Function

    Public Function FindText(ByVal value As String) As String Implements ITextControlExtension.FindText
        Return String.Empty
    End Function
    
End Class
